﻿//var $j = jQuery.noConflict();

/* uncomment this block if there are some conflicts with other js library/framework */
(function ($) {

AIC.AicValidation = {

    message: {
//        text: "",
        prefix: "",
        suffix: ""
    },

    init: function ( prefix , suffix ) {
        this.message.prefix = typeof (prefix) != 'undefined' ? prefix : "" ;
        this.message.suffix = typeof (suffix) != 'undefined' ? suffix : "" ;

        $.validator.addMethod (
            "exactlength"
            , function(value, element, param) {
                return this.optional(element) || value.length == param;
            }
            , jQuery.format("length must be {0} characters.")
        );

        $.validator.addMethod(
            "alphanumeric"
            , function(value, element) {
                return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
            }
            , "must contain only letters, numbers, or dashes."
        );


        $.validator.addMethod(
            "numeric"
            , function (value, element) { 
                var result = !isNaN(value); 
                return this.optional(element) || result; 
            }
            , "not Number" 
        );

        $.validator.addMethod(
            "numericmaxvalue"
            , function (value, element, param) { 
                var result = false; 
                if ( !isNaN(value) == false )
                    result = false;
                if ( !isNaN(param.maxvalue) == false )
                    result = false; 
                else if ( value > param.maxvalue )
                    result = false;
                else
                    result = true;

                return this.optional(element) || result; 
            }
            , "not Number" 
        );


        $.validator.addMethod(
            "numericminvalue"
            , function (value, element, param) { 
                var result = false; 
                if ( !isNaN(value) == false )
                    result = false;
                if ( !isNaN(param.minvalue) == false )
                    result = false; 
                else if ( value < param.minvalue )
                    result = false;
                else
                    result = true;

                return this.optional(element) || result; 
            }
            , "not Number" 
        );

//        $.validator.addMethod (
//            "username"
//            , function(value, element, param) {
//                //var nric = '[a-zA-Z]{1}[0-9]{7}[a-zA-Z]{1}';
//                var nric = /^([a-zA-Z]{1}[0-9]{7}[a-zA-Z]{1})$/ ;
//                return this.optional(element) || value.match (nric) ;
//            }
//            , jQuery.format("Must be the following format: `S` followed by 7 numeric characters followed by an alphabetic character")
//        );

//        $.validator.addMethod (
//            "shifttiming"
//            , function(value, element, param) {
//                var ret = false;
//
//                if ( param.startTime == "" && param.endTime == "" )
//                    ret = true;
//                else if ( param.startTime != "" && param.endTime != "" )
//                    ret = true;
//
//                return this.optional(element) || ret ;
//            }
//            , jQuery.format("Both Start Time & End Time are Must Blank Or Both are Must Defined")
//        );

    },


    format: function ( text ) {
        return $.format( this.message.prefix + text + this.message.suffix );
    },


    rules: {
//        //checkPostal: function ( selector , title ) {
//        checkPostal: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Postal Code" ;
//            var ret = {
//                required: true,
//                exactlength: 6,
//                numeric: true,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
////                    required: function( value , obj ){
////                        return AIC.AicValidation.format ( obj.title + " Must be Filled" );
////                    },
//                    exactlength: AIC.AicValidation.format ( title + " Must {0} characters" ),
//                    numeric: AIC.AicValidation.format ( title + " Must Numeric" )
//                }
//            };
//            return ret;
//        },
//
//        checkBlock: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Block" ;
//            var ret = {
//                required: true,
//                maxlength: 6,
//                alphanumeric: true,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ),
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" ),
//                    alphanumeric: AIC.AicValidation.format ( title + " Must be Alphanumeric" )
//                }
//            };
//            return ret;
//        },
//
//        checkUserName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "User Name" ;
//            var ret = {
//                required: true,
//                username: true,
//                maxlength: 9,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
//                    username: AIC.AicValidation.format ( title + " Format invalid. Accepted example : S1234567C " ),
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkStreetName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Street Name" ;
//            var ret = {
//                required: true,
//                maxlength: 100,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkFloorNo: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Floor No" ;
//            var ret = {
//                required: true,
//                maxlength: 4,
//                alphanumeric: true,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" ),
//                    alphanumeric: AIC.AicValidation.format ( title + " Must be Alphanumeric" )
//                }
//            };
//            return ret;
//        },
//
//        checkUnitNo: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Unit No" ;
//            var ret = {
//                required: true,
//                maxlength: 6,
//                alphanumeric: true,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
//                    maxlength: AIC.AicValidation.format ( title + "Unit Filled Max {0} characters" ),
//                    alphanumeric: AIC.AicValidation.format ( title + " Must be Alphanumeric" )
//                }
//            };
//            return ret;
//        },
//
//        checkBuildingName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Building Name" ;
//            var ret = {
//                maxlength: 100,
//                messages: {
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkStallNumber: function ( title , stallNo ) {
//            title = typeof (title) != 'undefined' ? title : "Stall No" ;
//            stallNo = typeof (stallNo) != 'undefined' ? stallNo : "" ;
//            title = title + " " + stallNo ;
//            var ret = {
//                exactlength: 8,
//                numeric: true,
//                messages: {
//                    exactlength: AIC.AicValidation.format ( title + " Must {0} characters" ),
//                    numeric: AIC.AicValidation.format ( title + " Must Numeric" )
//                }
//            };
//            return ret;
//        },
//
//
//        checkDescription: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Description" ;
//            var ret = {
//                maxlength: 100,
//                messages: {
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Name" ;
//            var ret = {
//                required: true,
//                maxlength: 40,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ),
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkPhone: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Phone No" ;
//            var ret = {
//                required: true,
//                exactlength: 8,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ) ,
//                    exactlength: AIC.AicValidation.format ( title + " Must {0} characters" )
//                }
//            };
//            return ret;
//        },
//
//        checkTeamName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Team Name" ;
//            var ret = {
//                required: true,
//                maxlength: 10,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ),
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },

//        checkShiftName: function ( title ) {
//            title = typeof (title) != 'undefined' ? title : "Shift Name" ;
//            var ret = {
//                required: true,
//                maxlength: 20,
//                messages: {
//                    required: AIC.AicValidation.format ( title + " Must be Filled" ),
//                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
//                }
//            };
//            return ret;
//        },

//        checkShiftTiming: function ( title , startTime , endTime ) {
//            title = typeof (title) != 'undefined' ? title : "Shift Timing" ;
//            var ret = {
//                shifttiming: { 
//                    startTime: startTime,
//                     endTime: endTime
//                    },
//                messages: {
//                    //shifttiming: AIC.AicValidation.format ( "" ),
//                }
//            };
//            return ret;
//        },


        checkRequired: function ( title ) {
            title = typeof (title) != 'undefined' ? title : "This" ;
            var ret = {
                required: true,
                messages: {
                    required: AIC.AicValidation.format ( title + " Must be Filled" )
                }
            };
            return ret;
        },

        checkNumber: function ( title ) {
            title = typeof (title) != 'undefined' ? title : "This" ;
            var ret = {
                numeric: true,
                messages: {
                    numeric: AIC.AicValidation.format ( title + " Must Numeric" )
                }
            };
            return ret;
        },

        checkMinLength: function ( title , maxlength) {
            title = typeof (title) != 'undefined' ? title : "This" ;
            var ret = {
                minlength: maxlength,
                messages: {
                    maxlength: AIC.AicValidation.format ( title + " Filled Max {0} characters" )
                }
            };
            return ret;
        },

//////        checkMinLength: function ( title , minlength) {
//////            title = typeof (title) != 'undefined' ? title : "This" ;
//////            var ret = {
//////                minlength: minlength,
//////                messages: {
//////                    minlength: AIC.AicValidation.format ( title + " Filled Min {0} characters" )
//////                }
//////            };
//////            return ret;
//////        },

        checkNumberMaxValue: function ( title , maxvalue) {
            title = typeof (title) != 'undefined' ? title : "This" ;
            var ret = {
                numericmaxvalue: { maxvalue: maxvalue },
                messages: {
                    numericmaxvalue: AIC.AicValidation.format ( title + " Filled Max Value = " + maxvalue )
                }
            };
            return ret;
        },

        checkNumberMinValue: function ( title , minvalue) {
            title = typeof (title) != 'undefined' ? title : "This" ;
            var ret = {
                numericminvalue: { minvalue: minvalue },
                messages: {
                    numericminvalue: AIC.AicValidation.format ( title + " Filled Min Value = " + minvalue )
                }
            };
            return ret;
        }
    }

};


/* uncomment this block if there are some conflicts with other js library/framework */
})(jQuery);